var _Messages = require('config/messages.js');

// ==========================
// BackListener
// ==========================
function BackListener() {
    this.mActive = false;
    this.mBlock = true;
    this.mScene = null;
}
BackListener.prototype.bindScene = function (scene) {
    //console.log('BackListener.prototype.bindScene', scene);
    this.mScene = scene;
    this.mBlock = false;
};
/**
 * let the system do back process
 * @param active
 */
BackListener.prototype.setActive = function (active) {
    this.mActive = active;
    _Messages.handleBack(this.mActive);
    if (!this.mActive) {
        this.mScene = null;
    }
};
BackListener.prototype.setBlocking = function (block) {
    this.mBlock = block;
    //console.log('BackListener.prototype.setBlocking', this.mBlock);
    if (block) {
        this.mScene = null;
    }
};
BackListener.prototype.processBackClick = function () {
    //console.log('BackListener.prototype.processBackClick', this.mScene);
    this.mScene.onBackPressed();
};
BackListener.prototype.onBackPressed = function (global) {
    //console.log('BackListener.prototype.onBackPressed', this.mBlock, this.mActive);
    if (this.mBlock || !this.mActive) {
        return;
    }
    // process in opengl thread
    global.scheduleTask(this.processBackClick, this);
};

module.exports = new BackListener();